package com.jy.adscm.operation.hr.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hr.bean.HrRecord;
import com.jy.adscm.operation.hr.service.HrRecordService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人力资源-记录 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/adscm/operation/hrRecord")
public class HrRecordController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    HrRecordService hrRecordService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody HrRecord hrRecord, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        hrRecord.setCreator(token.getUser().getId().toString());
        hrRecord.setCreateTime(new Date());
        boolean insert = hrRecordService.insertHrRecord(hrRecord);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody HrRecord hrRecord, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        hrRecord.setCreator(token.getUser().getId().toString());
        hrRecord.setCreateTime(new Date());
        boolean insert = hrRecordService.updateHrRecord(hrRecord);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = hrRecordService.deletedelete(id);
        return new SimpleCoreResponse("200","删除成功",insert);
    }
    @RequestMapping("selectByHid/{hid}/{type}")
    public SimpleCoreResponse selectByHid(@PathVariable Integer hid, @PathVariable Integer type, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<HrRecord> hrRecords = hrRecordService.selectByHid(hid,type);
        return new SimpleCoreResponse("200","查询成功",hrRecords);
    }
}

